package com.stray.crawl.worker.task;

import java.util.Random;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.stray.crawl.ResultContext;
import com.stray.crawl.TaskContext;
import com.stray.crawl.supt.EventPublisher;
import com.stray.crawl.worker.TaskStatus;

public class DebugCrawlTask extends CrawlTask {

	private final Log log = LogFactory.getLog(getClass());

	public DebugCrawlTask(EventPublisher<CrawlTask, TaskStatus> publisher, TaskContext taskContext) {
		super(publisher, taskContext);
	}

	@Override
	public ResultContext call() throws Exception {
		Random random = new Random();
		int t = random.nextInt(5000) + 5000;

		log.info("> TaskContext : " + ToStringBuilder.reflectionToString(taskContext, ToStringStyle.SIMPLE_STYLE));
		log.info("> Delay time  : " + t);

		Thread.sleep(t);

		log.info("> Delay finished");

		publisher.onEventRaise(this, TaskStatus.COMPLETE);
		publisher.onCallback(this);
		return this.resultContext;
	}

}